<template>
    <div class="app-container">
        <div>
            <p class="dialogTitle title">当前地震事件:
                <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
                <span v-else>请在地震事件管理中设置当前地震事件</span>
            </p>
        </div>

        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px" v-show="showSearch">
            <el-form-item label="抽样点编号" prop="samplingPointCode">
                <el-input v-model="queryParams.samplingPointCode" placeholder="请输入抽样点编号" clearable />
            </el-form-item>
            <el-form-item label="抽样点名称" prop="samplingPointName">
                <el-input v-model="queryParams.samplingPointName" placeholder="请输入抽样点名称" clearable />
            </el-form-item>
            <el-form-item label="调查员" prop="createUserName">
                <el-input v-model="queryParams.createUserName" placeholder="请输入调查员" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
        	<el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent" @click="handleImport">导入</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent||dataList.length==0" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe>
            <el-table-column label="序号" align="center" width="55">
                <template #default="scope">
                    <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="抽样点编号" align="center" prop="samplingPointCode">
                <template #default="scope">
                    <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{
                            scope.row.samplingPointCode
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column label="抽样点名称" align="center" prop="samplingPointName" />
            <el-table-column label="详细地址" align="center" prop="address" show-overflow-tooltip />
            <el-table-column label="经度" align="center" prop="longitude" />
            <el-table-column label="纬度" align="center" prop="latitude" />
            <el-table-column label="调查员" align="center" prop="createUserName" />
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />


        <el-dialog title="查看抽样点数据" v-model="open" width="900px" append-to-body>
            <el-form ref="addRef" label-width="120px" :model="form">
                <el-row style="margin: 20px 0;">
                    <el-col :span="24">
                        <el-form-item label="行政区划" required>
                            <span>{{ form.province }}{{ form.city }}{{ form.area }}{{ form.address }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="抽样点编号" prop="samplingPointCode" required>
                            <el-input v-model="form.samplingPointCode" placeholder="请输入抽样点编号" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="抽样点名称" prop="samplingPointName" required>
                            <el-input v-model="form.samplingPointName" placeholder="请输入抽样点名称" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="调查员" prop="createUserName" required>
                            <el-input v-model="form.createUserName" placeholder="请输入调查员" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="经度" prop="longitude" required>
                            <el-input v-model="form.longitude" placeholder="请输入经度" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="纬度" prop="latitude" required>
                            <el-input v-model="form.latitude" placeholder="请输入纬度" disabled />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="open = false">关 闭</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 选择评估子区对话框 -->
        <el-dialog title="选择调查任务" v-model="selectArea" width="1200px" append-to-body>
        	<p></p>
        	<el-table v-loading="loading1" :data="dataList1" stripe row-key="id" @row-click="singleElection">
            <el-table-column align="center" width="80" label="请选择">
				      <template #default="scope">
				      <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
				      <el-radio
				      class="radio"
				      v-model="templateSelection"
				      :label="scope.row.xitmId"
				      ><span></span></el-radio>
				      </template>
      			</el-table-column>
            
            <el-table-column label="调查任务编号" align="center" prop="taskCode" />
            <el-table-column label="调查任务名称" align="center" prop="taskName" />
            <!-- <el-table-column label="评估子区类别" align="center" prop="evaluationSubareaType" /> -->
            <el-table-column label="创建人" align="center" prop="createUserName" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
        	</el-table>
        	<pagination v-show="total1 > 0" :total="total1" v-model:page="queryParams1.page" v-model:limit="queryParams1.rows"
            @pagination="getList1" />
        	<template #footer>
              <div class="dialog-footer">
                  <el-button type="primary" @click="nextStep">下一步</el-button>
                  <el-button @click="selectArea = false">取 消</el-button>
              </div>
          </template>
        </el-dialog>
        <!-- 导入对话框 -->
        <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
          <p></p>
          <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
              :disabled="upload.isUploading" :before-upload="handleFileBeforeUpload"
              :on-progress="handleFileUploadProgress" :on-error="handleFileUploadError" :on-success="handleFileSuccess"
              :data="{ 'xsemId': currentEvent.id , 'xitmId': templateSelection  }" :auto-upload="false" drag>
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <template #tip>
                  <div class="el-upload__tip text-center">
                      <span>仅允许导入xls、xlsx格式文件。</span>
                       <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                          @click="importTemplate('抽样点数据.xlsx')">下载模板</el-link> 
                  </div>
              </template>
          </el-upload>
          <template #footer>
              <div class="dialog-footer">
                  <el-button type="primary" @click="submitFileForm">确 定</el-button>
                  <el-button @click="upload.open = false">取 消</el-button>
              </div>
          </template>
        </el-dialog>
    </div>
</template>
  
<script setup name="samplePointCheck">
import useEventStore from "@/store/modules/event";
import { getAllList ,getDetail, getTaskListByXsemIdAndType } from "@/api/disasterManage/fieldSurveyManage/samplePointCheck.js"
const { proxy } = getCurrentInstance();
import { getToken } from "@/utils/auth";
const dataList = ref([]);
const dataList1 = ref([]);
const ids = ref();
const loading = ref(false);
const loading1 = ref(false);
const single = ref(true);
const multiple = ref(true);
const selectArea = ref(false);
const open = ref(false);
const total = ref(0);
const total1 = ref(0);
const showSearch = ref(true);
const templateSelection = ref("");
const currentEvent = computed(() => useEventStore().currentEvent);
/*** 导入参数 */
const upload = reactive({
    open: false,
    title: "",
    isUploading: false,
    headers: { token: "token " + getToken() },
    url: import.meta.env.VITE_APP_BASE_API + "/samPoiData/importExcel"
});
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        samplingPointCode: undefined,
        samplingPointName: undefined,
        createUserNameName: undefined,
    },
    queryParams1: {
        page: 1,
        rows: 10,
    },
    form:{}
})
const { queryParams, queryParams1, form } = toRefs(data)

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() { 
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
    proxy.downloadTemplate('/samPoiData/exportExcel', {...queryParams.value, xsemId: currentEvent.value.id }, `抽样点数据.xls`)
}
// 导出模版
function importTemplate(name) {
    proxy.downloadTemplate("/template/download", {
        fileName: name
    }, name);
};


function singleElection(row) {
  templateSelection.value = row.xstmId;
  //console.log(templateSelection.value)

}

/** 提交上传文件 */
function submitFileForm() {
    proxy.$refs["uploadRef"].submit();
};

/** 打开选择评估子区弹窗 */
function handleImport() {
	selectArea.value = true;    
  getList1();
    
}
function getList1(){
	if (currentEvent.value) {
      loading1.value = true;
    //   getTaskListByXsemIdAndType({ ...queryParams1.value, xsemId: currentEvent.value.id, type:"1" }).then(response => {
      getTaskListByXsemIdAndType({ ...queryParams1.value, xsemId: currentEvent.value.id }).then(response => {
          dataList1.value = response.data.rows ? response.data.rows : [];
          total1.value = response.data.total;
          loading1.value = false;
      });
  } else {
      dataList1.value = [];
      total1.value = 0;
  }
}
/** 打开导入表弹窗 */
function nextStep() {
	if(!templateSelection.value){
		proxy.$modal.msgWarning("请选择评估子区");
		return;
	}
  upload.title = "导入";
  upload.open = true;
}
/**文件上传前处理 */
function handleFileBeforeUpload(file) {
    proxy.$modal.loading("正在上传文件，请稍候...");
    return true
}

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
    upload.isUploading = true;
};
// 上传失败
function handleFileUploadError(err) {
    proxy.$modal.closeLoading();
    proxy.$modal.msgError("上传文件失败");
}
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
    upload.open = false;
    upload.isUploading = false;
    proxy.$modal.closeLoading();
    proxy.$refs["uploadRef"].handleRemove(file);
    proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +  response.data?response.data:response.message + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
    getList();
};
// 编辑
function handleEdit(row){
  detail(row.id);
}

// 查看
function detail(id){
  getDetail({id:id}).then(res=>{
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
  })
}

/** 查询数据列表 */
function getList() {
    if (currentEvent.value) {
        loading.value = true;
        getAllList({ ...queryParams.value, xsemId: currentEvent.value.id }).then(response => {
            dataList.value = response.data.rows ? response.data.rows : [];
            total.value = response.data.total;
            loading.value = false;
        });
    } else {
        dataList.value = [];
        total.value = 0;
    }
}

onMounted(() => {
    setTimeout(() => {
        getList();
    }, 200)
})
</script>
  
<style scoped lang="scss">
.title {
    margin: 0 0 20px !important;
}
</style>
  
  